package com.ztgf.order.service;

import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.order.mapper.OrderCommissionStatisticsMapper;
import com.ztgf.order.models.bo.OrderCommissionStatisticsBO;
import com.ztgf.order.models.service.IOrderCommissionStatisticsService;
import com.ztgf.order.models.vo.OrderCommissionVO;
import com.ztgf.order.models.vo.OrderSalesCategoryShopVO;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import static com.ztgf.common.utils.UtilConstants.TWO;
import static com.ztgf.common.utils.UtilConstants.isNull;

/**
 * @author: chenyu.wang
 * @date: 2020-08-10
 * @description:后台管理系统佣金统计服务
 */
@DubboService
public class OrderCommissionStatisticsServiceImpl implements IOrderCommissionStatisticsService {

    @Autowired
    private OrderCommissionStatisticsMapper orderCommissionStatisticsMapper;


    @Override
    public String queryCommissionStatistics(OrderCommissionStatisticsBO bo) {
        BigDecimal price = orderCommissionStatisticsMapper.queryCommissionStatistics(bo);
        return price.setScale(TWO, BigDecimal.ROUND_HALF_UP).toString();
    }

    @Override
    public Page<OrderCommissionVO> queryCategoryMoney(OrderCommissionStatisticsBO bo) {
        Page<OrderCommissionVO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        //查询所有类目
        List<OrderCommissionVO> list = orderCommissionStatisticsMapper.queryAllCategory(page);
        if (isNull(list)) {
            return null;
        }
        for (OrderCommissionVO vo : list) {
            bo.setCategoryId(vo.getCategoryId());
            vo.setMoney(orderCommissionStatisticsMapper.queryForVariousItems(bo));
        }
        Collections.sort(list, new Comparator<OrderCommissionVO>() {
            @Override
            public int compare(OrderCommissionVO o1, OrderCommissionVO o2) {
                if (o1.getMoney().compareTo(o2.getMoney()) > 0) {
                    return -1;
                }
                if (o1.getMoney().equals(o2.getMoney())) {
                    return 0;
                }
                return 1;
            }
        });
        page.setRecords(list);
        return page;
    }

    @Override
    public Page<OrderCommissionVO> queryShopCommission(OrderCommissionStatisticsBO bo) {
        Page<OrderCommissionVO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        //查询店铺
        List<OrderCommissionVO> list = orderCommissionStatisticsMapper.queryAllShop(page);
        if (isNull(list)) {
            return null;
        }
        for (OrderCommissionVO vo : list) {
            vo.setMoney(orderCommissionStatisticsMapper.queryShopCommission(vo.getShopId(), bo));
        }
        list.sort(new Comparator<OrderCommissionVO>() {
            public int compare(OrderCommissionVO o1, OrderCommissionVO o2) {
                if (o1.getMoney().compareTo(o2.getMoney()) > 0) {
                    return -1;
                }
                if (o1.getMoney().equals(o2.getMoney())) {
                    return 0;
                }
                return 1;
            }
        });
        page.setRecords(list);
        return page;
    }

    @Override
    public Page<OrderCommissionVO> queryNew(OrderCommissionStatisticsBO bo) {
        Page<OrderCommissionVO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        List<OrderCommissionVO> list = orderCommissionStatisticsMapper.queryNew(page, bo);
        page.setRecords(list);
        return page;
    }
}
